import { http } from '@renderer/plugins/axios'
import router from '@renderer/plugins/router'
import { RouteLocationRaw } from 'vue-router'
export default () => {
  function request(fn: (args?: any) => Promise<any>) {
    return (args?: any) => {
      return fn(args)
    }
  }
  function open(url: RouteLocationRaw | string, target = '_self') {
    if (typeof url != 'string') url = router.resolve(url).fullPath
    if (target == '_blank') window.open(url)
    else location.href = url
  }

  const uploadFile = request(async (sendRes: { file: any; fileName: string }) => {
    try {
      await http.request({
        url: '/uploadFile',
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        data: sendRes.file,
      })
    } catch (error) {
      console.log(error)
    }
  })

  const handlePic = (fileUrl: string, file_type?: number) => {
    if (file_type == 1) {
      return new URL('@renderer/public/fileDir.jpg', import.meta.url).href
    }
    const fileType = fileUrl.split('.').pop()
    switch (fileType) {
      case 'jpg':
      case 'png':
      case 'webp':
        return fileUrl
      case 'xls':
      case 'xlsx':
        return new URL('@renderer/public/file/xls.jpg', import.meta.url).href
      case 'doc':
      case 'docx':
        return new URL('@renderer/public/file/doc.jpg', import.meta.url).href
      case 'rar':
      case 'zip':
        return new URL('@renderer/public/file/zip.jpg', import.meta.url).href
      case 'dwg':
      case 'mp3':
      case 'mp4':
      case 'txt':
      case 'max':
        return new URL(`../public/file/${fileType}.jpg`, import.meta.url).href
      case 'ppt':
      case 'pptx':
        return new URL('@renderer/public/file/ppt.jpg', import.meta.url).href
      default:
        return new URL('@renderer/public/file/none.jpg', import.meta.url).href
    }
  }
  const handleHousePic = (index: number) => {
    return new URL(`../public/house/icon${index + 1}.png`, import.meta.url).href
  }
  const handleGetIdentity = async () => {
    const userIdentity = await http.request<UserIdentityModel>({
      url: '/getIdentityStatus',
      method: 'GET',
    })
    delete userIdentity.id
    delete userIdentity.company_id
    delete userIdentity.assistant
    const relation = {
      business: '业务员',
      designer: '设计师',
      talk_order: '谈单手',
      project_manage: '项目经理',
      accountant: '会计',
      teller: '出纳',
      warehouse: '仓管',
      materials: '材料员',
      foreground: '前台',
      admin: '行政',
      supervisor: '监理',
      engineer_manage: '工程部经理',
      business_manage: '业务部经理',
      designer_manage: '设计部经理',
      boss: '总经理',
      other_boss: '其他经理',
      other_identity: '其他身份',
    }
    const indentityList = Object.keys(userIdentity).reduce<string[]>((acc, key) => {
      if (userIdentity[key] === 1) {
        acc.push(relation[key])
      }
      return acc
    }, [])
    return indentityList
  }
  return {
    open,
    request,
    handleGetIdentity,
    handlePic,
    uploadFile,
    handleHousePic,
  }
}
